<?php

use kartik\detail\DetailView;
use backend\models\Country;
use yii\helpers\Url;
use yii\widgets\Pjax;
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\widgets\Select2;
use kartik\file\FileInput;

$modelCountry = new Country();
$arr_provinces = $modelCountry->getProvinces();
if ($model->company_province) {
    $province = implode('', $modelCountry->getProvinces("id =" . $model->company_province));
} else {
    $province = '';
}
if ($model->company_city) {
    $city = implode('', $modelCountry->getProvinces("id=" . $model->company_city));
} else {
    $city = '';
}

$js = <<<JS
    $(function () {
        $('form').find('checkbox,radio,textarea').each(function () {
            $(this).wrap("<div class='col-sm-10'></div>")
        })
        $('.form-group > div').each(function () {
            $(this).addClass('col-sm-10')
        })

        $('.help-block').each(function () {
            $(this).addClass('col-sm-offset-2')
        })

        $('.form-group > input[type!=hidden]').each(function () {
            $(this).wrap("<div class='col-sm-10'></div>")
        })

        $('.form-group > label').each(function () {
            $(this).addClass('col-sm-2')
        });
    });
    $(".radio").addClass("radio-inline")
    
   
JS;
$js_check_company = <<<CHECK_COMPANY
$('#customer-company').blur(function(){
url = $('#chk-company-unique').val()
$.post(url,{"company":$(this).val()},function(data){
if(data == true){
 $('#customer-company').next().html('机构名称已录过系统，请确认是否录入')
 $('#customer-company').next().show()
}else{

}
});
});
CHECK_COMPANY;
$js_contact = <<<CONTACT
function addRow(item) {
    var html_tr ='<tr data-key="1">'+
    '<td class="skip-export kv-align-center kv-align-middle kv-row-select" style="width:50px;" data-col-seq="0"><input class="kv-row-checkbox" name="selection[]" value="1" type="checkbox"></td>'+
    '<td data-col-seq="1"><input id="customercontact-contact" class="form-control" name="CustomerContact[][contact]" placeholder="联系人" aria-required="true" aria-invalid="true" type="text"></td>'+
    '<td data-col-seq="2"><input id="customercontact-post" class="form-control" name="CustomerContact[][post]" placeholder="职务" aria-required="true" aria-invalid="true" type="text"></td>'+
    '<td data-col-seq="3"><input id="customercontact-mobile" class="form-control" name="CustomerContact[][mobile]" placeholder="电话" aria-required="true" aria-invalid="true" type="text"></td>'+
    '<td data-col-seq="4"><input id="customercontact-image" name="CustomerContact[][image]" aria-invalid="false" type="file"></td>'+
    '<td><span class="btn btn-default" onclick="addRow($(this))">ADD</span></td></tr>'
    
    item.parent().parent().after(html_tr)
}
CONTACT;
$js_add_contact = <<<JS
function addNewRow(item) {
  var i=$("#counter").val()
  var html_contact ='<div class="row form-inline"><div class="col-sm-offset-0 col-sm-10"><div class="form-group field-customercontact-'+i+'-contact required"><div class="col-sm-6 col-sm-offset-3">' +
   '<input id="customercontact-'+i+'-contact" class="form-control" name="CustomerContact['+i+'][contact]" placeholder="联系人" type="text"><div class="help-block help-block-error "></div></div></div>'+
            '<div class="form-group field-customercontact-'+i+'-post required"><div class="col-sm-6 col-sm-offset-3">' +
             '<input id="customercontact-'+i+'-post" class="form-control" name="CustomerContact['+i+'][post]" placeholder="职务" type="text"><div class="help-block help-block-error "></div></div></div>' +
              '<div class="form-group field-customercontact-'+i+'-mobile required"><div class="col-sm-6 col-sm-offset-3">' +
               '<input id="customercontact-'+i+'-mobile" class="form-control" name="CustomerContact['+i+'][mobile]" placeholder="电话" type="text"><div class="help-block help-block-error "></div></div></div>' +
                '<div class="form-group field-customercontact-'+i+'-image"><div class="col-sm-6 col-sm-offset-3">' +
                 '<input name="CustomerContact['+i+'][image]" value="" type="hidden"><input id="customercontact-'+i+'-image" name="CustomerContact['+i+'][image]" placeholder="名片" type="file"><div class="help-block help-block-error "></div></div></div>' +
                  '</div></div>'
  item.parent().parent().after(html_contact)
  $('#counter').val(parseInt(i)+1)
}
JS;
$this->registerJs($js_check_company, \yii\web\View::POS_END);
$this->registerJs($js_add_contact, \yii\web\View::POS_END);
?>
<div class="container">
    <div class="project-ease-form">

        <?php
        Pjax::begin(['id' => 'project_form']);
        echo DetailView::widget([
            'model' => $model,
            'condensed' => true,
            'hover' => true,
            'mode' => 'edit',
            'panel' => [
                'heading' => '编辑客户信息',
                'headingOptions' => ['template' => '{title}'],
                'type' => DetailView::TYPE_PRIMARY,
                'footer' => '<span class="glyphicon glyphicon-pencil"></span>',
                'footerOptions' => ['class' => 'panel-heading', 'tag' => 'h4', 'template' => '{buttons}{title}']
            ],
            'formOptions' => ['options' => ['data-pjax' => true],],
            'hideIfEmpty' => false,//如果有空字段则隐藏
            'buttons1' => '{update}',
            'buttons2' => '{view} {reset} {save}',
            'updateOptions' => ['label' => '<span class="btn btn-info">编辑</span>'],
            'viewOptions' => ['label' => '<span class="btn btn-info">查看</span>'],
            'saveOptions' => ['label' => $model->id == '' ? '<span class="btn btn-info">保存</span>' : (($model->id && $model->package->owner == Yii::$app->user->id) ? '<span class="btn btn-info">保存</span>' : '')],
            'resetOptions' => ['label' => '<span class="btn btn-info">重置</span>'],
            'attributes' => [
                [
                    'group' => true,
                    'label' => '基础信息',
                    'rowOptions' => ['class' => 'info']
                ],
                'company',
                [
                    'columns' => [
                        [
                            'attribute' => 'company_province',
                            'type' => DetailView::INPUT_SELECT2,
                            'value' => $province,
                            'widgetOptions' => [
                                'data' => $arr_provinces,
                                'options' => ['placeholder' => '选择省份 ...'],
                                'pluginOptions' => [
                                    'allowClear' => true
                                ]
                            ],
                            'label' => "<span class='text-danger'>" . $model->getAttributeLabel('company_province') . "*</span>",
                            'valueColOptions' => ['style' => 'width:30%']
                        ],
                        [
                            'attribute' => 'company_city',
                            'type' => DetailView::INPUT_DEPDROP,
                            'widgetOptions' => [
                                'type' => 2,
                                'pluginOptions' => [
                                    'depends' => ['customer-company_province'],
                                    'placeholder' => '选择城市...',
                                    'url' => Url::to(['/country/get-customer-city',]),
                                ]
                            ],
                            'value' => $city,
                        ],

                    ],
                ],
                [
                    'columns' => [
                        [
                            'attribute' => 'company_type',
                            'type' => DetailView::INPUT_SELECT2,
                            'widgetOptions' => [
                                'data' => Yii::$app->params['company_type'],
                            ]
                        ],
                        [
                            'attribute' => 'company_level',
                            'type' => DetailView::INPUT_SELECT2,
                            'widgetOptions' => [
                                'data' => Yii::$app->params['company_level'],
                            ]
                        ],
                    ]
                ],
                [
                    'group' => true,
                    'label' => '投资需求',
                    'rowOptions' => ['class' => 'info']
                ],
                [
                    'attribute' => 'is_need_clear',
                    'type' => DetailView::INPUT_SWITCH,
                    'format' => 'raw',
                    'value' => $model->is_need_clear ? '<span class="label label-success">是</span>' : '<span class="label label-danger">否</span>',
                    'widgetOptions' => [
                        'pluginOptions' => [
                            'onText' => '是',
                            'offText' => '否',
                        ]
                    ]
                ],
                'tag',
                [
                    'attribute' => 'need',
                    'type' => DetailView::INPUT_TEXTAREA,
                ],
                [
                    'attribute' => 'market_area',
                    'type' => DetailView::INPUT_TEXTAREA,
                ],
                [
                    'group' => true,
                    'label' => '客户信息',
                    'rowOptions' => ['class' => 'info']
                ],
                [
                    'columns' => [
                        [
                            'attribute' => 'type_desc',
                            'type' => DetailView::INPUT_SELECT2,
                            'widgetOptions' => [
                                'data' => Yii::$app->params['type_desc'],
                                'pluginOptions' => [
                                    'multiple' => true,
                                    'allowClear' => true,
                                ]
                            ]
                        ],
                        [
                            'attribute' => 'province',
                            'type' => DetailView::INPUT_SELECT2,
                            'widgetOptions' => [
                                'data' => $arr_province = Country::localCity('`pid`=0 AND `order`>0'),
                                'pluginOptions' => [
                                    'multiple' => true,
                                    'allowClear' => true,
                                ]
                            ]
                        ]
                    ]
                ],
                [
                    'group' => true,
                    'label' => '资产包信息',
                    'rowOptions' => ['class' => 'info']
                ],
                'principal',
                [
                    'attribute' => 'package_type',
                    'type' => DetailView::INPUT_SELECT2,
                    'widgetOptions' => [
                        'data' => Yii::$app->params['package_type'],
                    ]
                ],
                [
                    'attribute' => 'package_transfer',
                    'type' => DetailView::INPUT_SELECT2,
                    'widgetOptions' => [
                        'data' => Yii::$app->params['package_transfer'],
                    ]
                ],
                [
                    'attribute' => 'project_province',
                    'type' => DetailView::INPUT_SELECT2,
                    'widgetOptions' => [
                        'data' => Country::localCity('`pid`=0 AND `order`>0'),
                    ]
                ],
                [
                    'attribute' => 'project_file',
                    'type' => DetailView::INPUT_FILEINPUT,
                    'widgetOptions' => [

                    ]
                ],

            ],
        ]);

        Pjax::end();
        ?>

        <?php
        //    Pjax::begin([
        //        //Pjax options
        //    ]);
        echo "<input type='hidden' id='counter' value='1' />";
        echo "<input type='hidden' id='chk-company-unique' value='" . \yii\helpers\Url::to(['customer/company-unique']) . "'>";

        $form = ActiveForm::begin([
            'layout' => 'horizontal',
            'options' => ['enctype' => 'multipart/form-data', 'data' => ['pjax' => false],]
        ]); ?>

        <div class="col-md-offset-0" id="contact-info">
            <h4 class="text-info ">联系人信息</h4>
        </div>
        <?php echo $this->render('contact-list', ['contactDataProvider' => $contactDataProvider]) ?>
        <div class="row form-inline">
            <div class="col-sm-offset-0 col-sm-10">
                <?= $form->field($contactModel, '[0]contact')->textInput(['placeholder' => '联系人'])->label(false) ?>
                <?= $form->field($contactModel, '[0]post')->textInput(['placeholder' => '职务'])->label(false) ?>
                <?= $form->field($contactModel, '[0]mobile')->textInput(['placeholder' => '电话'])->label(false) ?>
                <?= $form->field($contactModel, '[0]image')->fileInput(['placeholder' => '名片', 'maxlength' => '1', 'size' => 10])->label(false) ?>

            </div>
            <label class="btn btn-info" onclick="addNewRow($(this))">继续添加</label>
        </div>
        <?php
        ActiveForm::end();
        //    Pjax::end();

        ?>
    </div>
</div>